﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Threading;

using PickGold.Callers;
using PickGold.Handler;

namespace PickGold.Arguments
{
	/// <summary>
	/// 异步操作错误事件参数
	/// </summary>
	[Serializable]
	[ComVisible(true)]
	[ClassInterface(ClassInterfaceType.AutoDispatch)]
	public class AsyncErrorEventArgs : EventArgs, IAsyncResult
	{
		private Exception _Exception;
		private IAsyncResult _Result;
		private AsyncCallback _Callback;
		private Delegate _EndCallback;
		private object _Target;
		private object _Data;

		/// <summary>
		/// 构造一个 AsyncErrorEventArgs 类型的实例
		/// </summary>
		/// <param name="error">异步异常</param>
		/// <param name="result">异步状态</param>
		public AsyncErrorEventArgs(Exception error, IAsyncResult result) : this(error, result, null, null, null) { }
		/// <summary>
		/// 构造一个 AsyncErrorEventArgs 类型的实例
		/// </summary>
		/// <param name="error">异步异常</param>
		/// <param name="result">异步状态</param>
		/// <param name="target">操作目标</param>
		public AsyncErrorEventArgs(Exception error, IAsyncResult result, object target) : this(error, result, target, null, null) { }
		/// <summary>
		/// 构造一个 AsyncErrorEventArgs 类型的实例
		/// </summary>
		/// <param name="error">异步异常</param>
		/// <param name="result">异步状态</param>
		/// <param name="target">操作目标</param>
		/// <param name="callback">回调委托</param>
		public AsyncErrorEventArgs(Exception error, IAsyncResult result, object target, AsyncCallback callback) : this(error, result, target, callback, null) { }
		/// <summary>
		/// 构造一个 AsyncErrorEventArgs 类型的实例
		/// </summary>
		/// <param name="error">异步异常</param>
		/// <param name="result">异步状态</param>
		/// <param name="target">操作目标</param>
		/// <param name="callback">回调委托</param>
		/// <param name="endCallback">操作目标的回调方法</param>
		public AsyncErrorEventArgs(Exception error, IAsyncResult result, object target, AsyncCallback callback, Delegate endCallback)
			: base()
		{
			this._Exception = error;
			this._Result = result;
			this._Target = target;
			this._Callback = callback;
			this._EndCallback = endCallback;
		}

		#region IAsyncResult

		object IAsyncResult.AsyncState
		{
			get
			{
				return this._Result.AsyncState;
			}
		}

		WaitHandle IAsyncResult.AsyncWaitHandle
		{
			get
			{
				return this._Result.AsyncWaitHandle;
			}
		}

		bool IAsyncResult.CompletedSynchronously
		{
			get
			{
				return this._Result.CompletedSynchronously;
			}
		}

		bool IAsyncResult.IsCompleted
		{
			get
			{
				return this._Result.IsCompleted;
			}
		}

		#endregion

		/// <summary>
		/// 获取异步异常
		/// </summary>
		public Exception AsyncError
		{
			get
			{
				return this._Exception;
			}
		}

		/// <summary>
		/// 获取异步状态
		/// </summary>
		public IAsyncResult AsyncResult
		{
			get
			{
				return this._Result;
			}
		}

		/// <summary>
		/// 获取回调委托
		/// </summary>
		public AsyncCallback Callback
		{
			get
			{
				return this._Callback;
			}
		}

		/// <summary>
		/// 获取操作目标的回调方法
		/// </summary>
		public Delegate EndCallback
		{
			get
			{
				return this._EndCallback;
			}
		}

		/// <summary>
		/// 获取操作目标
		/// </summary>
		public object Target
		{
			get
			{
				return this._Target;
			}
		}

		/// <summary>
		/// 获取或设置附加数据
		/// </summary>
		public object Data
		{
			set
			{
				this._Data = value;
			}
			get
			{
				return this._Data;
			}
		}
	}
}
